var o = require;
var t = module;
cc.Class({
    extends: cc.Component,
    properties: {wordRoot: cc.Node, prefabWord: cc.Prefab, orderTip: cc.Node},
    onDestroy: function onDestroy() {
        this.winTimeout && clearTimeout(this.winTimeout);
    },
    start: function start() {
        var o = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this),
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this),
            this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this),
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this),
            (this.level = lGlobal.curLevel);
        var t = lGlobal.lvCfg.json[lGlobal.curModel][lGlobal.curLevel];
        this.sounds = [];
        var d = lGlobal.modelBundles[lGlobal.curModel];
        if (d) {
            var i = t.lv;
            d.loadDir("data/" + i, null, function (d, i) {
                if (d) console.log(d);
                else {
                    for (var e = 0; e < i.length; e++) {
                        if ("answer" == i[e].name) t.answer = i[e].text.split("\r\n");
                        else if ("words" == i[e].name) t.words = i[e].text.split("\r\n");
                        else if (0 == i[e].name.indexOf("sound")) {
                            var s = Number(i[e].name.substring("sound".length));
                            (o.sounds[s] = o.node.addComponent(cc.AudioSource)), (o.sounds[s].clip = i[e]);
                        }
                    }
                    (o.dcWordTotal = []),
                        (o.eliminate = t.eliminate),
                        (o.order = t.order),
                        (o.orderIndex = 0),
                        o.order ? (o.orderTip.active = !0) : (o.orderTip.active = !1),
                        (o.dcAnswers = JSON.parse(JSON.stringify(t.answer))),
                        (o.dcWordMap = []);
                    for (var r = 0; r < t.words.length; r++) {
                        o.dcWordMap[r] = [];
                        for (var c = 0; c < t.words[r].length; c++) {
                            var h = t.words[r][c],
                                n = cc.instantiate(o.prefabWord);
                            n.parent = o.wordRoot;
                            var a = n.getComponent("DcWord");
                            a.setWord(h), (o.dcWordMap[r][c] = a), o.dcWordTotal.push(a);
                        }
                    }
                    (o.dcWordTouchStart = null),
                        (o.dcWordsTouched = []),
                        (o.dcWordLastTouched = null),
                        (o.dcWordWidth = o.dcWordTotal[0].node.width),
                        (o.dcWordHeight = o.dcWordTotal[0].node.height),
                        (o.ccLeft = 0),
                        (o.ccTop = 0),
                        (o.spacingX = 0),
                        (o.spacingY = 0);
                    var l = o.wordRoot.getComponent(cc.Layout);
                    l &&
                        ((o.ccLeft = l.paddingLeft),
                        (o.ccTop = l.paddingTop),
                        (o.spacingX = l.spacingX),
                        (o.spacingY = l.spacingY),
                        setTimeout(function () {
                            l.enabled = !1;
                        }, 200)),
                        t.lineCount && (o.wordRoot.width = t.lineCount * (o.dcWordWidth + o.spacingX) - o.spacingX);
                }
            });
        }
        this.curSelectColor = null;
    },
    onTouchStart: function onTouchStart(o) {
        this.curSelectColor = lGlobal.getNextWordBgColor();
        var t = o.getLocation(),
            d = this.getDcWordByLocation(t);
        d &&
            !d.slined &&
            ((this.inDrag = !0),
            (this.dcWordTouchStart = d),
            this.dcWordTouchStart.selected(this.curSelectColor),
            this.dcWordsTouched.indexOf(this.dcWordTouchStart) < 0 && this.dcWordsTouched.push(this.dcWordTouchStart),
            (this.dcWordLastTouched = this.dcWordTouchStart));
    },
    onTouchMove: function onTouchMove(o) {
        if (this.inDrag) {
            var t = o.getLocation(),
                d = this.getDcWordByLocation(t);
            if (d && !d.slined)
                if (this.dcWordTouchStart) {
                    if (this.dcWordLastTouched != d && this.isNeighbor(this.dcWordLastTouched, d))
                        if (d.snext == this.dcWordLastTouched) {
                            d.snext.unselected();
                            var i = this.dcWordsTouched.indexOf(d.snext);
                            i >= 0 && this.dcWordsTouched.splice(i, 1),
                                (d.snext = null),
                                (this.dcWordLastTouched.sprev = null),
                                (this.dcWordLastTouched = d);
                        } else
                            d.isSelected ||
                                (d.selected(this.curSelectColor),
                                (d.sprev = this.dcWordLastTouched),
                                (this.dcWordLastTouched.snext = d),
                                this.dcWordsTouched.indexOf(d) < 0 && this.dcWordsTouched.push(d),
                                (this.dcWordLastTouched = d));
                } else
                    (this.dcWordTouchStart = d),
                        this.dcWordTouchStart.selected(this.curSelectColor),
                        this.dcWordsTouched.indexOf(this.dcWordTouchStart) < 0 &&
                            this.dcWordsTouched.push(this.dcWordTouchStart),
                        (this.dcWordLastTouched = this.dcWordTouchStart);
        }
    },
    onTouchEnd: function onTouchEnd(o) {
        this.inDrag = !1;
        var t = o.getLocation(),
            d = this.getDcWordByLocation(t);
        d &&
            !d.slined &&
            ((this.dcWordLastTouched = d), this.dcWordsTouched.indexOf(d) < 0 && this.dcWordsTouched.push(d));
        for (var i = "", e = 0; e < this.dcWordsTouched.length; e++) {
            i += this.dcWordsTouched[e].word;
        }
        var s = !1;
        if (this.order) i === this.dcAnswers[this.orderIndex] && (this.orderIndex++, (s = !0));
        else
            for (var r = 0; r < this.dcAnswers.length; r++) {
                if (i === this.dcAnswers[r]) {
                    (s = !0), this.sounds[r] && this.sounds[r].play();
                    for (var c = 0; c < this.dcAnswers.length; c++) {
                        c != r && this.sounds[c] && this.sounds[c].stop();
                    }
                }
            }
        if (s) {
            for (var h = 0; h < this.dcWordsTouched.length; h++) {
                this.eliminate
                    ? (this.dcWordTotal.indexOf(this.dcWordsTouched[h]),
                      this.dcWordsTouched[h].disappear(this.dcWordsTouched[h + 1] || this.dcWordsTouched[0]))
                    : this.dcWordsTouched[h].lined(this.dcWordsTouched[h + 1] || this.dcWordsTouched[0]);
            }
            soundMgr.playSound(soundMgr.right);
        } else soundMgr.playSound(soundMgr.wrong);
        this.clearTouched(), this.checkWin(), this.eliminate && (this.checkDrop(), this.checkMoveToLeft());
    },
    onTouchCancel: function onTouchCancel() {},
    clearTouched: function clearTouched() {
        for (var o = 0; o < this.dcWordsTouched.length; o++) {
            this.dcWordsTouched[o] &&
                (this.dcWordsTouched[o].unselected(),
                (this.dcWordsTouched[o].sprev = null),
                (this.dcWordsTouched[o].snext = null));
        }
        (this.dcWordTouchStart = null), (this.dcWordsTouched = []), (this.dcWordLastTouched = null);
    },
    getDcWordByLocation: function getDcWordByLocation(o) {
        for (var t = null, d = 0; d < this.dcWordTotal.length; d++) {
            this.dcWordTotal[d].hided ||
                (this.dcWordTotal[d].node.getBoundingBoxToWorld().contains(o)
                    ? ((t = this.dcWordTotal[d]), this.dcWordTotal[d].getForce())
                    : this.dcWordTotal[d].hasForce && this.dcWordTotal[d].loseForce());
        }
        return t;
    },
    isNeighbor: function isNeighbor(o, t) {
        var d = !1,
            i = this.getLogicPos(o.node.position),
            e = this.getLogicPos(t.node.position);
        return (
            (((i.x != e.x - 1 && i.x != e.x + 1) || i.y != e.y) &&
                ((i.y != e.y - 1 && i.y != e.y + 1) || i.x != e.x)) ||
                (d = !0),
            d
        );
    },
    getLogicPos: function getLogicPos(o) {
        var t = 0 - this.wordRoot.width * this.wordRoot.anchorX + this.dcWordWidth / 2 + this.ccLeft,
            d =
                this.wordRoot.height -
                this.wordRoot.height * this.wordRoot.anchorY -
                this.dcWordHeight / 2 +
                this.ccTop,
            i = o.x - t,
            e = d - o.y,
            s = Math.floor(i / (this.dcWordWidth + this.spacingX)),
            r = Math.floor(e / (this.dcWordHeight + this.spacingY));
        return cc.v2(s, r);
    },
    checkWin: function checkWin() {
        for (var o = this, t = !0, d = 0; d < this.dcWordTotal.length; d++) {
            this.dcWordTotal[d].slined || " " == this.dcWordTotal[d].label.string || (t = !1);
        }
        t &&
            (this.winTimeout = setTimeout(function () {
                window.ui.win({model: "DragConnect", lv: o.level});
            }, 1500));
    },
    checkDrop: function checkDrop() {
        for (var o = 0; o < this.dcWordMap[0].length; o++) {
            for (var t = this.getRawShowed(o), d = 0; d < t.length; d++) {
                var i = this.getPixelPosByLogicPos(o, this.dcWordMap.length - 1 - d);
                t[d].node.position = i;
            }
        }
    },
    checkMoveToLeft: function checkMoveToLeft() {
        for (var o = 0; o < this.dcWordMap[0].length; o++) {
            if (this.colEmpty(o))
                for (var t = 0; t < this.dcWordMap.length; t++) {
                    for (var d = o + 1; d < this.dcWordMap[t].length; d++) {
                        var i = this.getDcWordByLogicPos(d, t);
                        i && (i.node.position = this.getPixelPosByLogicPos(d - 1, t));
                    }
                }
        }
    },
    getRowShowed: function getRowShowed(o) {
        for (var t = [], d = this.dcWordMap[o].length, i = 0; i < d; i++) {
            var e = this.getDcWordByLogicPos(i, o);
            e && !e.hided && t.push(e);
        }
        return t;
    },
    getRawShowed: function getRawShowed(o) {
        for (var t = [], d = this.dcWordMap.length - 1; d > -1; d--) {
            var i = this.getDcWordByLogicPos(o, d);
            i && !i.hided && t.push(i);
        }
        return t;
    },
    colEmpty: function colEmpty(o) {
        for (var t = !0, d = 0; d < this.dcWordMap.length; d++) {
            if (this.getDcWordByLogicPos(o, d)) {
                t = !1;
                break;
            }
        }
        return t;
    },
    getDcWordByLogicPos: function getDcWordByLogicPos(o, t) {
        for (var d = null, i = 0; i < this.dcWordTotal.length; i++) {
            if (!this.dcWordTotal[i].hided) {
                var e = this.getLogicPos(this.dcWordTotal[i].node.position);
                if (e.x == o && e.y == t) {
                    d = this.dcWordTotal[i];
                    break;
                }
            }
        }
        return d;
    },
    getPixelPosByLogicPos: function getPixelPosByLogicPos(o, t) {
        var d = 0 - this.wordRoot.width * this.wordRoot.anchorX + this.dcWordWidth / 2 + this.ccLeft,
            i =
                this.wordRoot.height -
                this.wordRoot.height * this.wordRoot.anchorY -
                this.dcWordHeight / 2 +
                this.ccTop,
            e = o * (this.dcWordWidth + this.spacingX) + d,
            s = i - t * (this.dcWordHeight + this.spacingY);
        return cc.v2(e, s);
    },
    onOrderTipClick: function onOrderTipClick() {
        this.sounds[this.orderIndex]
            ? this.sounds[this.orderIndex].play()
            : console.warn("DragConnect顺序模式无语音提示。");
    }
});
